package com.aiju.cash.ui.login;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;

import com.aiju.base.activity.BaseTitleActivity;
import com.aiju.bean.User;
import com.aiju.cash.R;
import com.aiju.cash.core.api.DataJson;
import com.aiju.cash.core.api.ShopApi;
import com.aiju.cash.ui.login.manager.LoginCheck;
import com.aiju.cash.ui.login.manager.LoginManager;
import com.aiju.utils.L;
import com.aiju.utils.T;
import com.zhy.http.okhttp.callback.StringCallback;

import okhttp3.Call;

/**
 * @author: 佐为
 * @date: 2016-11-10 14:42
 * @version: V1.0
 * @Description: 创建店铺页面
 */
public class CreateShopActivity extends BaseTitleActivity {

    private EditText shopNameEditText;

    private String shopId, userName, password;

    private int type;
    private String shopName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_create_shop);
        setCenterTitle("创建店铺");

        shopId = getIntent().getStringExtra("shopId");
        userName = getIntent().getStringExtra("userName");
        password = getIntent().getStringExtra("password");

        type = getIntent().getIntExtra("type", 0);
        shopNameEditText = (EditText) findViewById(R.id.et_shop_name);
        findViewById(R.id.tv_shop_next).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String shopName = shopNameEditText.getText().toString().trim();
                L.d(shopName);
                if (type == 1) {
                    User user = (User) getIntent().getSerializableExtra("user");
                    if (!TextUtils.isEmpty(shopName)){
                        if (LoginCheck.checkShopName(shopName)) {
                            saveShopName(user.getUserName(), shopName);
                        } else {
                            T.ToastShort("店铺名称仅支持1-15位汉字、字母、数字及下划线，请重新输入");
                        }
                    }else {
                        T.ToastShort("已使用您的手机号作为店铺名称");
                        saveShopName(user.getUserName(), user.getUserName());
                    }
                    L.d("创建店铺");
                } else {
                    if (!TextUtils.isEmpty(shopName)) {
                        if (LoginCheck.checkShopName(shopName)) {
                            updateShopName(shopId, shopName);
                        } else {
                            T.ToastShort("店铺名称仅支持1-15位汉字、字母、数字及下划线，请重新输入");
                        }
                    } else {
                        T.ToastShort("已使用您的手机号作为店铺名称");
                        new LoginManager(CreateShopActivity.this, mDialogHelper).login(userName, password);
                    }
                    L.d("修改店铺");
                }
            }
        });


        mAiJuTitleBarView.setLeftOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (type != 1) {
                    new LoginManager(CreateShopActivity.this, mDialogHelper, false).login(userName, password);
                    onBackPressed();
                } else {
                    startActivity(LoginActivity.class);
                    finish();
                }
            }
        });

    }

    private void saveShopName(final String userName, final String shopName) {
        mDialogHelper.showLoadingProgressDialog();
        new ShopApi().saveShop(userName, shopName, new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                T.ToastNetError();
                mDialogHelper.dismissProgressDialog();
            }

            @Override
            public void onResponse(String response, int id) {
                mDialogHelper.dismissProgressDialog();
                DataJson dataJson = new DataJson(response);
                if (dataJson.isSuccess()) {
                    new LoginManager(CreateShopActivity.this, mDialogHelper, false).login(userName, password);
                } else {
                    T.ToastShort(dataJson.getMsg());
                }
            }
        });
    }

    private void updateShopName(String shopId, String shopName) {
        mDialogHelper.showLoadingProgressDialog();
        new ShopApi().updateShop(shopId, shopName, new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                T.ToastNetError();
                mDialogHelper.dismissProgressDialog();
            }

            @Override
            public void onResponse(String response, int id) {
                mDialogHelper.dismissProgressDialog();
                DataJson dataJson = new DataJson(response);
                if (dataJson.isSuccess()) {
                    new LoginManager(CreateShopActivity.this, mDialogHelper, false).login(userName, password);
                } else {
                    T.ToastShort(dataJson.getMsg());
                }
            }
        });
    }
}
